package edu.spec.sms.dao.impl;

import edu.spec.sms.dao.iface.IStudentDao;
import edu.spec.sms.model.Student;
import org.madgecko.basic.dao.BaseDao;
import org.madgecko.basic.model.Pager;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentDao extends BaseDao<Student> implements IStudentDao {
	@Override
	public Student loadByXh(String xh) {
		String hql = "from Student s where s.xh=?";
		return (Student)this.queryObject(hql, xh);
	}

    @Override
    public Student loadByXhAndSfzh(String xh, String sfzh) {
        String hql = " from Student s where s.xh=? and s.sfzh=?";
        String[] queryParam = {xh, sfzh};
        return (Student) this.queryObject(hql, queryParam);
    }

    @Override
	public Student loadByOpenId(String openId) {
		String hql = "from Student s where s.openId=?";
		return (Student)this.queryObject(hql, openId);
	}

	@Override
	public Pager<Student> findStudentByCon(Integer status, Integer clazzId, String con) {
		String hql = " from Student s where 1=1";
		if(status!=null){
			hql += " and s.status=" + status;
		}
		if(clazzId!=null){
			hql += " and s.clazz.id=" + clazzId;
		}
		if(con!=null&&!"".equals(con.trim())){
			hql += " and s.name like '%" + con + "%' or s.xh like '%" + con + "%'";
		}
		return this.find(hql);
	}

	@Override
	public List<Student> listStudentByClazz(Integer clazzId) {
		String hql = " from Student s where s.clazz.id=" + clazzId;
		return this.list(hql);
	}

	@Override
	public List<Integer> listStudentIdByClazz(Integer clazzId) {
		String hql = "select s.id from Student s where s.clazz.id=" + clazzId;
		return this.listIdByHql(hql);
	}
}
